package com.jwd.avmanager.repository;

import com.jwd.avmanager.util.DebugLogger;

import java.sql.*;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Description: 数据库数据处理
 * @Author: david
 * @CreateTime: 2022/4/21
 * @Company: jwd
 */
public class DBManager {
    public static final String CONNECTION_URL = "jdbc:sqlserver://119.23.66.152:1433;databaseName=zfy4G";
    public static final String PASSWORD = "6zhCcRa15Zi3Rj6z";
//    public static final String CONNECTION_URL = "jdbc:sqlserver://127.0.0.1:1433;databaseName=zfy4g2";
//    public static final String PASSWORD = "Server2019";
    public static final String USER = "sa";


    private static DBManager instance;
    Connection con = null;
    private Timer mTimer;
    private TimerTask mConnTask = new TimerTask() {
        @Override
        public void run() {
            try {
                if (con != null && con.isClosed()) {
                    connect();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    };

    public DBManager() {
        connect();
        mTimer = new Timer();
        mTimer.schedule(mConnTask, 60 * 60 * 1000, 60 * 60 * 1000);
    }

    private void connect() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(CONNECTION_URL, USER, PASSWORD);
            DebugLogger.info("数据库连接成功");
        } catch (ClassNotFoundException e) {    
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static DBManager getInstance() {
        if (instance == null) {
            synchronized (DBManager.class) {
                if (instance == null) {
                    instance = new DBManager();
                }
            }
        }
        return instance;
    }


//    public void select (String username) throws  SQLException{
//        try(
//            CallableStatement cstat = con.prepareCall("{call TerminalLoginProc(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}")) {
//            cstat.setString(1,"");
//            cstat.setString(2,"jwd001");
//            cstat.setString(3,"");
//            cstat.setString(4,"");
//            cstat.setInt(5,0);
//            cstat.setInt(6,0);
//            cstat.setString(7,"");
//            cstat.setInt(8,0);
//            cstat.setInt(9,0);
//            cstat.setInt(10,0);
//            cstat.setInt(11,0);
//            cstat.setInt(12,0);
//            cstat.setInt(13,0);
//            cstat.setInt(14,0);
//            cstat.setString(15,"");
//            cstat.setString(16,"");
//            cstat.setInt(17,0);
//            DebugLogger.debug(cstat.execute());
//            ResultSet rs = cstat.getResultSet();
//            DebugLogger.debug( rs == null );
//            while(rs.next()) {
//                DebugLogger.debug(rs.getString(1) + "\t" + rs.getInt(2));
//            }
//            DebugLogger.debug("DBManager.select");
//        }
//    }


}
